home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / HDF / dfp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-20  |  10.4 KB  |  353 lines

  1. /*****************************************************************************
  2. *              NCSA HDF version 3.10
  3. *                July 1, 1990
  4. *
  5. * NCSA HDF Version 3.10 source code and documentation are in the public
  6. * domain.  Specifically, we give to the public domain all rights for future
  7. * licensing of the source code, all resale rights, and all publishing rights.
  8. * We ask, but do not require, that the following message be included in all
  9. * derived works:
  10. * Portions developed at the National Center for Supercomputing Applications at
  11. * the University of Illinois at Urbana-Champaign.
  12. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  13. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  14. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  15. *****************************************************************************/
  16.  
  17. #ifdef RCSID
  18. static char RcsId[] = "@(#)$Revision: 3.1 $";
  19. #endif
  20. /*
  21. $Header: /pita/work/HDF/dev/RCS/src/dfp.c,v 3.1 90/07/02 11:52:10 clow beta $
  22.  
  23. $Log:    dfp.c,v $
  24.  * Revision 3.1  90/07/02  11:52:10  clow
  25.  * some cosmetic modifications
  26.  * 
  27.  * Revision 3.0  90/02/02  20:30:53  clow
  28.  * *** empty log message ***
  29.  * 
  30. */
  31.  
  32. /*-----------------------------------------------------------------------------
  33.  * File:    dfp.c
  34.  * Purpose: read and write palettes
  35.  * Invokes: df.c
  36.  * Contents: 
  37.  *  DFPgetpal: retrieve next palette
  38.  *  DFPputpal: write palette to file
  39.  *  DFPaddpal: add palette to file
  40.  *  DFPnpals: number of palettes in HDF file
  41.  *  DFPreadref: get palette with this reference number next
  42.  *  DFPwriteref: put palette with this reference number next
  43.  *  DFPrestart: forget info about last file accessed - restart from beginning
  44.  *  DFPlastref: return reference number of last element read or written
  45.  *  DFPIopen: open/reopen file
  46.  *---------------------------------------------------------------------------*/
  47.  
  48. #include "df.h"
  49.  
  50. static uint16 Readref=0;
  51. static uint16 Writeref=0;
  52. static uint16 Refset=0;        /* Ref of palette to get next */
  53. static uint16 Lastref = 0;    /* Last ref read/written */
  54.  
  55. static char Lastfile[DF_MAXFNLEN]; /* last file opened */
  56.  
  57. #ifndef VMS
  58. DF *DFPIopen();
  59. #else /*VMS*/
  60. DF *_DFPIopen();
  61. #endif
  62.  
  63.  
  64. /*-----------------------------------------------------------------------------
  65.  * Name:    DFPgetpal
  66.  * Purpose: get next palette from file
  67.  * Inputs:  filename: name of HDF file
  68.  *          palette: 768 byte space to read palette into
  69.  * Returns: 0 on success, -1 on failure with DFerror set
  70.  *          palette in pal
  71.  * Users:   HDF HLL users, utilities, other routines
  72.  * Invokes: DFPIopen, DFIerr, DFclose, DFgetelement
  73.  *---------------------------------------------------------------------------*/
  74.  
  75. int DFPgetpal(filename, palette)
  76. char *filename;
  77. char *palette;
  78. {
  79.     DF *dfile;
  80.     int cdd, ret;
  81.     DFdle *dlep;
  82.  
  83.     DFerror = DFE_NOERROR;
  84.  
  85.     if (!palette) {
  86.         DFerror = DFE_BADPTR;
  87.         return(-1);
  88.     }
  89.     dfile = DFPIopen(filename, DFACC_READ);
  90.     if (dfile == NULL) return(-1);
  91.  
  92.     if (Refset)
  93.         ret = DFIfind(dfile, DFTAG_IP8, Refset, 1,(uint16) 0, (uint16) 0,
  94.               &dlep, &cdd);
  95.     else
  96.         ret = DFIfind(dfile,DFTAG_IP8,0, !Readref, DFTAG_IP8,Readref,
  97.               &dlep,&cdd);
  98.  
  99.     Refset = 0;
  100.     if (ret<0) return(DFIerr(dfile)); /* on error, close file and return -1 */
  101.  
  102.     Readref = dlep->dd[cdd].ref; /* ref of element to read */
  103.  
  104.         /* read palette */
  105.     if (DFgetelement(dfile, DFTAG_IP8, Readref, palette)<0)
  106.             return(DFIerr(dfile));
  107.  
  108.     Lastref = Readref;
  109.  
  110.     return(DFclose(dfile));
  111. }
  112.  
  113.  
  114. /*-----------------------------------------------------------------------------
  115.  * Name:    DFPputpal
  116.  * Purpose: Write palette to file
  117.  * Inputs:  filename: name of HDF file
  118.  *          palette: palette to be written to file
  119.  *          overwrite: if 1, overwrite last palette read or written
  120.  *                     if 0, write it as a fresh palette
  121.  *          filemode: if "a", append palette to file
  122.  *                    if "w", create new file
  123.  * Returns: 0 on success, -1 on failure with DFerror set
  124.  * Users:   HDF users, programmers, utilities
  125.  * Invokes: DFPIopen, DFclose, DFputelement, DFIerr
  126.  * Remarks: To overwrite, the filename must be the same as for the previous
  127.  *          call
  128.  *---------------------------------------------------------------------------*/
  129.  
  130. int DFPputpal(filename, palette, overwrite, filemode)
  131. char *filename;
  132. char *palette;
  133. int overwrite;
  134. char *filemode;
  135. {
  136.     DF *dfile;
  137.  
  138.     DFerror = DFE_NOERROR;
  139.  
  140.     if (!palette) {
  141.         DFerror = DFE_BADPTR;
  142.         return(-1);
  143.     }
  144.  
  145.     if (overwrite && strcmp(filename, Lastfile)) {
  146.         DFerror = DFE_BADCALL;
  147.         return(-1);
  148.     }
  149.  
  150.     dfile = DFPIopen(filename, (*filemode=='w') ? DFACC_CREATE : DFACC_WRITE);
  151.     if (dfile==NULL) return(-1);
  152.  
  153.         /* if we want to overwrite, Lastref is the ref to write.  If not, if
  154.             Writeref is set, we use that ref.  If not we get a fresh ref. The
  155.             ref to write is placed in Lastref */
  156.     if (!overwrite) Lastref = Writeref ? Writeref : DFnewref(dfile);
  157.     if (Lastref == 0) return(-1);
  158.  
  159.     Writeref = 0;           /* don't know ref to write after this */
  160.  
  161.         /* write out palette */
  162.     if (DFputelement(dfile, DFTAG_IP8, Lastref, palette, (int32) 768)<0)
  163.             return(DFIerr(dfile));
  164.  
  165.     return(DFclose(dfile));
  166. }
  167.  
  168.  
  169. /*-----------------------------------------------------------------------------
  170.  * Name:    DFPaddpal
  171.  * Purpose: Add palette to file
  172.  * Inputs:  filename: name of HDF file
  173.  *          palette: palette to be written to file
  174.  * Returns: 0 on success, -1 on failure with DFerror set
  175.  * Users:   HDF users, programmers, utilities
  176.  * Invokes: DFPputpal
  177.  *---------------------------------------------------------------------------*/
  178.  
  179. int DFPaddpal(filename, palette)
  180. char *filename;
  181. char *palette;
  182. {
  183.  
  184.     return(DFPputpal(filename, palette, 0, "a"));
  185. }
  186.  
  187.  
  188. /*-----------------------------------------------------------------------------
  189.  * Name:    DFPnpals
  190.  * Purpose: How many palettes are present in this file?
  191.  * Inputs:  filename: name of HDF file
  192.  * Returns: number of palettes on success, -1 on failure with DFerror set
  193.  * Users:   HDF programmers, other routines and utilities
  194.  * Invokes: DFPIopen, DFclose, DFnumber
  195.  *---------------------------------------------------------------------------*/
  196.  
  197. int DFPnpals(filename)
  198. char *filename;
  199. {
  200.     DF *dfile;
  201.     int npals=0;
  202.  
  203.     DFerror = DFE_NOERROR;
  204.  
  205.     /* should use reopen if same file as last time - more efficient */
  206.     dfile = DFPIopen(filename, DFACC_READ);
  207.     if (dfile==NULL) return(-1);
  208.  
  209.     npals = DFnumber(dfile, DFTAG_IP8);    /* count number of IPs */
  210.     if (npals<0) return(DFIerr(dfile));
  211.  
  212.     if (DFclose(dfile)<0) return(-1);
  213.     return(npals);
  214. }
  215.  
  216.  
  217. /*-----------------------------------------------------------------------------
  218.  * Name:    DFPreadref
  219.  * Purpose: Set ref of palette to get next
  220.  * Inputs:  filename: file to which this applies
  221.  *          ref: reference number of next get
  222.  * Returns: 0 on success, -1 on failure
  223.  * Users:   HDF programmers, other routines and utilities
  224.  * Invokes: DFPIopen, DFIfind, DFclose
  225.  * Remarks: checks if palette with this ref exists
  226.  *---------------------------------------------------------------------------*/
  227.  
  228. int DFPreadref(filename, ref)
  229. char *filename;
  230. uint16 ref;
  231. {
  232.     DF *dfile;
  233.     int cdd;
  234.     DFdle *dlep;
  235.  
  236.     DFerror = DFE_NOERROR;
  237.  
  238.     dfile = DFPIopen(filename, DFACC_READ);
  239.     if (dfile==NULL) return(-1);
  240.  
  241.     if (DFIfind(dfile, DFTAG_IP8, ref, 1, 0, 0, &dlep, &cdd)<0)
  242.         return(DFIerr(dfile));
  243.  
  244.     Refset = ref;
  245.     return(DFclose(dfile));
  246. }
  247.  
  248.  
  249. /*-----------------------------------------------------------------------------
  250.  * Name:    DFPwriteref
  251.  * Purpose: Set ref of palette to put next
  252.  * Inputs:  filename: file to which this applies
  253.  *          ref: reference number of next put
  254.  * Returns: 0 on success, -1 on failure
  255.  * Users:   HDF programmers, other routines and utilities
  256.  * Invokes: none
  257.  *---------------------------------------------------------------------------*/
  258.  
  259. /* shut lint up */
  260. /* ARGSUSED */
  261. int DFPwriteref(filename, ref)
  262. char *filename;
  263. uint16 ref;
  264. {
  265.     DFerror = DFE_NOERROR;
  266.  
  267.     Writeref = ref;
  268.     return(0);
  269. }
  270.  
  271.  
  272.  
  273. /*-----------------------------------------------------------------------------
  274.  * Name:    DFPrestart
  275.  * Purpose: Do not remember info about file - get again from first palette
  276.  * Inputs:  none
  277.  * Returns: 0 on success
  278.  * Users:   HDF programmers
  279.  * Remarks: Just reset Lastfile to NULL
  280.  *---------------------------------------------------------------------------*/
  281.  
  282. int DFPrestart()
  283. {
  284.  
  285.     Lastfile[0] = '\0';
  286.     return(0);
  287. }
  288.  
  289.  
  290. /*-----------------------------------------------------------------------------
  291.  * Name:    DFPlastref
  292.  * Purpose: Return last ref written or read
  293.  * Inputs:  none
  294.  * Globals: Lastref
  295.  * Returns: ref on success, -1 on error with DFerror set
  296.  * Users:   HDF users, utilities, other routines
  297.  * Invokes: none
  298.  * Method:  return Lastref
  299.  * Remarks: none
  300.  *---------------------------------------------------------------------------*/
  301.  
  302. int DFPlastref()
  303. {
  304.  
  305.     return(Lastref);
  306. }
  307.  
  308.  
  309.  
  310. /******************************************************************************/
  311. /*----------------------- Internal routines ---------------------------------*/
  312. /******************************************************************************/
  313.  
  314.  
  315. /*-----------------------------------------------------------------------------
  316.  * Name:    DFPIopen
  317.  * Purpose: open or reopen a file
  318.  * Inputs:  filename: name of file to open
  319.  *          access : access mode
  320.  * Returns: file pointer on success, NULL on failure with DFerror set
  321.  * Users:   HDF systems programmers, other DFP routines
  322.  * Invokes: DFopen
  323.  * Remarks: This is a hook for someday providing more efficient ways to
  324.  *          reopen a file, to avoid re-reading all the headers
  325.  *---------------------------------------------------------------------------*/
  326.  
  327. DF *DFPIopen(filename, access)
  328. char *filename;
  329. int access;
  330. {
  331.  
  332.     DF *dfile;
  333.  
  334.         /* use reopen if same file as last time - more efficient */
  335.     if (strncmp(Lastfile,filename,DF_MAXFNLEN) || (access==DFACC_CREATE)) {
  336.                                     /* treat create as different file */
  337.         if (!(dfile = DFopen(filename, access, -1))) return(NULL);
  338.         Refset = 0;         /* no ref to get set for this file */
  339.         Readref = 0;
  340.     }
  341.     else
  342.         if (!(dfile = DFopen(filename, access, -1))) return(NULL);
  343.  
  344.     DFIstrncpy(Lastfile, filename, DF_MAXFNLEN);
  345.         /* remember filename, so reopen may be used next time if same file */
  346.     return(dfile);
  347. }
  348.